<%-- 
    Document   : history
    Created on : Dec 21, 2011, 4:41:07 PM
    Author     : XPYCT
--%>

<%@page import="manufacturerBean.*"%>
<%@page import="categoryBean.*"%>
<%@page import="DBConnection.ATeamObject"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.Iterator"%>
<%@page import="java.util.Collection"%>
<%@page import="javax.rmi.PortableRemoteObject"%>
<%@page import="historyBean.*"%>
<%@page import="goodsBean.*"%>
<%@page import="javax.naming.InitialContext"%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<%@ page errorPage = "Error.jsp" %>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>История изменения</title>
        <style type="text/css">
        #head { /* Верхний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        h1 {
        font-size: 36px; /* Размер шрифта */
        font-weight: bold; /* Жирное начертание */
        margin: 0; /* Убираем отступы */
        color: #fff; /* Цвет текста */
        }
        #foot { /* Нижний блок */
        background: #333; /* Цвет фона */
        padding: 5px; /* Поля вокруг текста */
        color: #fff; /* Цвет текста */
        clear: both; /* Отменяем обтекание */
        }
        </style>
    </head>
    <body background="1.jpg" bgproperties="fixed">
        <div id="head">
            <center>
            <h1>История изменения</h1>
            </center>
        </div>
        <h4><a href="index.jsp">Главная</a> /  <a href="history.jsp?type=<%=request.getParameter("type")%>">История изменения</a></h4>    
        <center>
            <%
                InitialContext initialContext = new InitialContext();
                Object historyhomeObject = initialContext.lookup("HistoryBean");
                HistoryHome historyHome = (HistoryHome)PortableRemoteObject.
                        narrow(historyhomeObject, HistoryHome.class);
                History historyObject = historyHome.create();
                
                Object goodsHomeObject = initialContext.lookup("GoodsBean");
                GoodsHome goodsHome = (GoodsHome)
                        PortableRemoteObject.narrow(
                        goodsHomeObject,GoodsHome.class);
                
                Object categoryHomeObject = initialContext.lookup(
                        "CategoryBean");
                CategoryHome categoryHome = (CategoryHome)
                        PortableRemoteObject.narrow(
                        categoryHomeObject,CategoryHome.class);
                
                Object manufacturerHomeObject = initialContext.lookup(
                        "ManufacturerBean");
                ManufacturerHome manufacturerHome = (ManufacturerHome)
                        PortableRemoteObject.narrow(
                        manufacturerHomeObject,ManufacturerHome.class);
                
                Collection<Integer> historyKeys = (Collection<Integer>)request.
                        getSession().getAttribute("historyKeys");
                Iterator hkIterator = historyKeys.iterator();
                while (hkIterator.hasNext()) {
                    Integer objectId = (Integer)hkIterator.next();
                    Collection<Integer> historyRecords =
                            (Collection<Integer>)historyObject.
                            loadHistory(objectId);
                    String objectName = null; 
                    switch (Integer.parseInt(request.getParameter("type"))) {
                    case 1: {
                        Category categoryObject = categoryHome.findByPrimaryKey(
                                objectId);
                        objectName = categoryObject.getName();
                    } break;
                    
                    case 2: {
                        Manufacturer manufacturerObject = manufacturerHome.
                                findByPrimaryKey(objectId);
                        objectName = manufacturerObject.getName();
                    } break;
                    
                    case 4: {
                        Goods goodsObject = goodsHome.findByPrimaryKey(objectId);
                        objectName = goodsObject.getNameOfGoods();
                    } break;
                }
                    
            %>
            <h2><%= objectName %></h2>
            <table border="1">
                <tr>
                    <th>Дата</th>
                    <th>Событие</th>
                    <th>Описание</th>
                </tr>
                <%
                        Iterator recIterator = historyRecords.iterator();
                        while (recIterator.hasNext()) {
                            String[] record = (String[]) recIterator.next();
                %>            
                <tr>
                    <td><%= record[2] %></td>
                    <td><%= record[0] %></td>
                    <td><%= record[1] %></td>
                </tr>
                <%            
                        }
                %>
            </table>    
            <%
                    }
            %>
        </center>    
    </body>
</html>
